﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Xml;

namespace Comm
{
    /// <summary>
    /// SEO站点地图操作类
    /// </summary>
    public class BSiteMap
    {
        public string path = null;
        /// <summary>
        /// 获取集合
        /// </summary>
        /// <param name="strWhere"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public List<MSiteMap> getList(string strWhere, string sort)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(path);
            if (ds.Tables.Count < 1)
            {
                return null;
            }

            DataRow[] rows = ds.Tables[0].Select(strWhere, sort);

            if (rows == null || rows.Length < 1)
            {
                return null;
            }

            List<MSiteMap> list = DataRowToList(rows);
            return list;
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="GID"></param>
        /// <returns></returns>
        public MSiteMap getModel(string loc)
        {
            List<MSiteMap> list = getList("loc='" + loc + "'", "lastmod desc");
            if (list == null || list.Count < 1)
            {
                return null;
            }
            return list[0];
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        public void Add(MSiteMap model)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);

            XmlNode UrlNode = doc.CreateElement("url");
            XmlNode LocNode = doc.CreateElement("loc");
            LocNode.InnerText = model.Loc;
            XmlNode LastModNode = doc.CreateElement("lastmod");
            LastModNode.InnerText = model.LastMod;
            XmlNode ChangeFregNode = doc.CreateElement("changefreg");
            ChangeFregNode.InnerText = model.ChangeFreg.ToString();
            XmlNode PriorityNode = doc.CreateElement("priority");
            PriorityNode.InnerText = model.Priority;

            UrlNode.AppendChild(LocNode);
            UrlNode.AppendChild(LastModNode);
            UrlNode.AppendChild(ChangeFregNode);
            UrlNode.AppendChild(PriorityNode);

            doc["urlset"].AppendChild(UrlNode);
            doc.Save(path);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void Update(MSiteMap model)
        {
            Delete(model.Loc);
            Add(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        public void Delete(string loc)
        {
            XmlDocument doc = XMLHelper.xmlDoc(path);
            XmlNode node = null;
            foreach (XmlNode nd in doc["urlset"].ChildNodes)
            {
                if (nd.Name == "url" && nd.ChildNodes[1].InnerText == loc)
                {
                    node = nd;
                }
            }
            if (node == null)
            {
                return;
            }
            doc["urlset"].RemoveChild(node);
            doc.Save(path);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        public void DeleteByHistory()
        {
            bool flag = false;
            DateTime nowdt = DateTime.Now.AddDays(-7);
            XmlDocument doc = XMLHelper.xmlDoc(path);
            foreach (XmlNode nd in doc["urlset"].ChildNodes)
            {
                DateTime dt = DateTime.Now;
                if (nd.Name == "url" && DateTime.TryParse(nd.ChildNodes[1].InnerText, out dt) && dt < nowdt)
                {
                    doc["urlset"].RemoveChild(nd);
                    flag = true;
                }
            }

            if (flag)
            {
                doc.Save(path);
            }
        }

        /// <summary>
        /// 将DataSet转成List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<MSiteMap> DataSetToList(DataSet ds)
        {
            if (ds == null || ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }

            List<MSiteMap> list = new List<MSiteMap>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                MSiteMap model = new MSiteMap();
                model.Loc = row["loc"].ToString().Trim();
                model.LastMod = row["lastmod"].ToString().Trim();
                model.ChangeFreg = (EChangeFreg)Enum.Parse(typeof(EChangeFreg), row["changefreg"].ToString());
                model.Priority = row["priority"].ToString().Trim();

                list.Add(model);
            }

            return list;
        }

        /// <summary>
        /// 将DataSet转成List
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        private List<MSiteMap> DataRowToList(DataRow[] rows)
        {
            if (rows == null || rows.Length < 1)
            {
                return null;
            }

            List<MSiteMap> list = new List<MSiteMap>();
            foreach (DataRow row in rows)
            {
                MSiteMap model = new MSiteMap();
                model.Loc = row["loc"].ToString().Trim();
                model.LastMod = row["lastmod"].ToString().Trim();
                model.ChangeFreg = (EChangeFreg)Enum.Parse(typeof(EChangeFreg), row["changefreg"].ToString());
                model.Priority = row["priority"].ToString().Trim();

                list.Add(model);
            }

            return list;
        }
    }

    /// <summary>
    /// SEO站点地图实体类
    /// </summary>
    public class MSiteMap
    {
        private string _loc;
        private string _lastmod;
        private EChangeFreg _changefreg;
        private string _priority;

        /// <summary>
        /// Url地址
        /// </summary>
        public string Loc
        {
            set { _loc = value; }
            get { return _loc; }
        }

        /// <summary>
        /// 最后一次修改时间
        /// </summary>
        public string LastMod
        {
            set { _lastmod = value; }
            get { return _lastmod; }
        }

        /// <summary>
        /// 页面的修改频率(always,hourly,daily,weekly,monthly,yearly,never)
        /// </summary>
        public EChangeFreg ChangeFreg
        {
            set { _changefreg = value; }
            get { return _changefreg; }
        }

        /// <summary>
        /// 重要性(0.1~1)
        /// </summary>
        public string Priority
        {
            set { _priority = value; }
            get { return _priority; }
        }
    }

    /// <summary>
    /// SEO站点地图页面修改平率枚举
    /// </summary>
    public enum EChangeFreg
    {
        /// <summary>
        /// 总是
        /// </summary>
        always = 1,
        /// <summary>
        /// 每小时
        /// </summary>
        hourly = 2,
        /// <summary>
        /// 每日
        /// </summary>
        daily = 3,
        /// <summary>
        /// 每周
        /// </summary>
        weekly = 4,
        /// <summary>
        /// 每月
        /// </summary>
        monthly = 5,
        /// <summary>
        /// 每年
        /// </summary>
        yearly = 6,
        /// <summary>
        /// 绝不
        /// </summary>
        never = 7
    }
}
